package com.oocl.homework.crm.model;


import com.oocl.homework.crm.datastruct.MyCompareable;

public class User implements MyCompareable<User>{
	
	private int id=-1;
	private String name;
	private String sex;
	private String birthday;
	private String address;
	private String phone;
	
	public User(){}
	
	
	//可改成builder
	public User(String[] parameter){
		id=-1;
		for (int i = 0; i < parameter.length; i++) {
			String key = parameter[i].split(":")[0];
			String value = parameter[i].split(":")[1];
			if (key.equals("id")) 
				id = Integer.valueOf(value);
			if (key.equals("name")) 
				name = value;
			if (key.equals("sex")) 
				sex = value;
			if (key.equals("birthday")) 
				birthday = value;
			if (key.equals("address")) 
				address = value;
			if (key.equals("phone")) 
				phone = value;
			
		}
		
	}
	
	public void setAttributes(String[] parameter){
		id = -1;
		for (int i = 0; i < parameter.length; i++) {
			String key = parameter[i].split(":")[0];
			String value = parameter[i].split(":")[1];
			if (key.equals("id")) 
				id = Integer.valueOf(value);
			if (key.equals("name")) 
				name = value;
			if (key.equals("sex")) 
				sex = value;
			if (key.equals("birthday")) 
				birthday = value;
			if (key.equals("address")) 
				address = value;
			if (key.equals("phone")) 
				phone = value;
			
		}
	}
	
	public User(int id, String name, String sex, String birthday, String address,
			String phone) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.birthday = birthday;
		this.address = address;
		this.phone = phone;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public String toString(){
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("id:"+id+";name:"+name);
		if (sex!=null) 
			stringBuffer.append(";sex:"+sex);
		if (birthday!=null) 
			stringBuffer.append(";birthday:"+birthday);
		if (address!=null) 
			stringBuffer.append(";address:"+address);
		if (phone!=null) 
			stringBuffer.append(";phone:"+phone);
		return stringBuffer.toString();
		
	}
	
	
	@Override
	public int compareIdTo(User user) {
		if (this.id<user.id) 
			return -1;
		if (this.id>user.id) 
			return 1;
		return 0;
	}
	
	@Override
	public int compareNameTo(User user) {
		return name.compareTo(user.name);
	}


	@Override
	public int meetTo(User user) {
		if(user.id!=-1&&user.id!=id)
			return -1;
		if(user.name!=null&&!user.name.equals(name))
			return -1;
		if(user.sex!=null&&!user.sex.equals(sex))
			return -1;
		if(user.birthday!=null&&!user.birthday.equals(birthday))
			return -1;
		if(user.address!=null&&!user.address.equals(address))
			return -1;
		if(user.phone!=null&&!user.phone.equals(phone))
			return -1;
			
		return 1;
	}
	
	@Override
	public boolean equals(Object user) {
		User user1 = (User) user;
		if (id == user1.id) {
			return true; 
		}
		return false;
	}



	
	

}
